package com.hd.pzhwlxt.utils;


import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUploadUtil {

    public static String saveFileAndReturnUrl(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String path = System.getProperty("user.dir");
        path=path+"\\files\\"+originalFilename;
        File file1 = new File(path);
        File parentFile = file1.getParentFile();

        //如果文件不存在，建立该文件夹
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String url="http://localhost/file/photo/"+originalFilename;
        return url;
    }

    public static File getFile(String photoname){
        String path = System.getProperty("user.dir");
        path = path + "\\files\\"+ photoname;
        return new File(path);
    }


}
